package org.neframework.test.db;

import org.neframework.test.model.TableColumnModel;

import com.tmsps.neframework.core.utils.ChkTools;

public class AlterTableColumnTools {

	// http://www.w3school.com.cn/sql/sql_alter.asp

	// String addColume = "ALTER TABLE Persons ADD Birthday date";
	// String alterColume = "ALTER TABLE Persons MODIFY Birthday year";
	// String dropColume = "ALTER TABLE Person DROP COLUMN Birthday";

	public String table;// 操作表
	public String alter;// 操作类型: [ADD] or [ALTER] or [DROP]
	public TableColumnModel alterColumn;// 要修改的列

	private static final String addColumeSql = "alter table @[table] add @[column] @[column_type]";
	private static final String alterColumeSql = "alter table @[table] modify @[column] @[column_type]";
	private static final String dropColumeSql = "alter table @[table] drop column @[column]";

	// 获取最终要操作的 sql
	public String getAlterSQL() {
		String sql = null;
		if (ChkTools.isNull(table) || ChkTools.isNull(alter) || alterColumn.isNull()) {
			throw new RuntimeException("获取 修改sql 有字段为空,校验失败,写代码注意点啊...");
		}
		if ("ADD".equals(alter)) {
			sql = addColumeSql;
		} else if ("ALTER".equals(alter)) {
			sql = alterColumeSql;
		} else if ("DROP".equals(alter)) {
			sql = dropColumeSql;
		} else {
			throw new RuntimeException("AlterTableModel-->alter 参数错误, 校验失败...");
		}

		sql = sql.replace("@[table]", alterColumn.getTable());
		sql = sql.replace("@[column]", alterColumn.getColumn());
		sql = sql.replace("@[column_type]", alterColumn.getAlterTypes());

		return sql;
	}

	public static void main(String[] args) {

	}

}
